package com.esri.android.oauth;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.net.http.SslError;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.webkit.HttpAuthHandler;
import android.webkit.SslErrorHandler;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.esri.core.internal.a;
import com.esri.core.internal.io.handler.d;
import com.esri.core.internal.tasks.e;
import com.esri.core.internal.tasks.oauth.GetTokenTaskParameters;
import com.esri.core.io.EsriSecurityException;
import com.esri.core.io.EsriServiceException;
import com.esri.core.io.UserCredentials;
import com.esri.core.map.CallbackListener;
import com.tencent.smtt.sdk.TbsListener;
import java.net.URI;
import org.kxml2.wap.Wbxml;

/* loaded from: classes.dex */
public class OAuthView extends WebView {
    private static final String a = "ags_http_auth_request_dialog_title";
    private static final String b = "Authentication Required";
    private static final String c = "ags_http_auth_request_dialog_username_hint";
    private static final String d = "Username";
    private static final String e = "ags_http_auth_request_dialog_password_hint";
    private static final String f = "Password";
    private static final String g = "ags_http_auth_request_dialog_button1";
    private static final String h = "Log in";
    private static final String i = "ags_http_auth_request_dialog_button2";
    private static final String j = "Cancel";
    private static final String k = "string";
    private static final String l = "clientid";
    private static final String m = "urn:ietf:wg:oauth:2.0:oob";
    private static final String t = "oauthview_email_subject";
    private static final String u = "Technical support";
    private static final String v = "oauthview_email_body";
    private static final String w = "What's your problem?";
    private static final String x = "oauthview_action_chooser_title";
    private static final String y = "Sending to";
    private static final String z = "support@esri.com";
    private String n;
    private String o;
    private CallbackListener<UserCredentials> p;
    private OnSslErrorListener q;
    private boolean r;
    private int s;

    /* loaded from: classes.dex */
    public interface OnSslErrorListener {
        void onReceivedSslError(OAuthView oAuthView, SslErrorHandler sslErrorHandler, SslError sslError);
    }

    public OAuthView(Context context, String str, String str2, CallbackListener<UserCredentials> callbackListener) {
        super(context);
        this.r = false;
        this.n = str2;
        this.o = str;
        this.p = callbackListener;
        this.r = false;
        a(context, (AttributeSet) null);
    }

    public OAuthView(Context context, String str, String str2, int i2, CallbackListener<UserCredentials> callbackListener) {
        super(context);
        this.r = false;
        this.n = str2;
        this.o = str;
        this.p = callbackListener;
        this.s = i2;
        this.r = true;
        a(context, (AttributeSet) null);
    }

    public OAuthView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        this.r = false;
        a(context, attributeSet);
    }

    public OAuthView(Context context, AttributeSet attributeSet, int i2) {
        super(context, attributeSet, i2);
        this.r = false;
        a(context, attributeSet);
    }

    private void a(final Context context, AttributeSet attributeSet) {
        if (attributeSet != null) {
            for (int i2 = 0; i2 < attributeSet.getAttributeCount(); i2++) {
                if (l.compareToIgnoreCase(attributeSet.getAttributeName(i2)) == 0) {
                    this.n = attributeSet.getAttributeValue(i2);
                } else if ("portalUrl".compareToIgnoreCase(attributeSet.getAttributeName(i2)) == 0) {
                    this.o = attributeSet.getAttributeValue(i2);
                }
            }
        }
        if (TextUtils.isEmpty(this.n)) {
            throw new EsriServiceException("OAuthView requires an App ID.");
        }
        if (TextUtils.isEmpty(this.o)) {
            throw new EsriServiceException("OAuthView requires a portal URL.");
        }
        getSettings().setJavaScriptEnabled(true);
        getSettings().setBuiltInZoomControls(true);
        setFocusable(true);
        setWebViewClient(new WebViewClient() { // from class: com.esri.android.oauth.OAuthView.1
            @Override // android.webkit.WebViewClient
            public void onPageFinished(WebView webView, String str) {
            }

            @Override // android.webkit.WebViewClient
            public boolean shouldOverrideUrlLoading(WebView webView, String str) {
                String[] strArr;
                try {
                    Uri parse = Uri.parse(str);
                    String scheme = parse.getScheme();
                    if (scheme == null) {
                        return false;
                    }
                    if (scheme.startsWith("http")) {
                        String queryParameter = parse.getQueryParameter("error");
                        String queryParameter2 = parse.getQueryParameter("error_description");
                        final String queryParameter3 = parse.getQueryParameter("code");
                        if (!TextUtils.isEmpty(queryParameter)) {
                            StringBuilder sb = new StringBuilder();
                            sb.append("Unable to retrieve authcode due to: ");
                            if (TextUtils.isEmpty(queryParameter2)) {
                                queryParameter2 = "unknown reason";
                            }
                            sb.append(queryParameter2);
                            sb.append(".");
                            EsriServiceException esriServiceException = new EsriServiceException(sb.toString());
                            if (OAuthView.this.p != null) {
                                OAuthView.this.p.onError(esriServiceException);
                            } else {
                                throw esriServiceException;
                            }
                        }
                        if (TextUtils.isEmpty(queryParameter3)) {
                            return false;
                        }
                        e.c.execute(new Runnable() { // from class: com.esri.android.oauth.OAuthView.1.1
                            @Override // java.lang.Runnable
                            public void run() {
                                try {
                                    UserCredentials a2 = OAuthView.this.a(OAuthView.this.a(), queryParameter3);
                                    if (OAuthView.this.p != null) {
                                        OAuthView.this.p.onCallback(a2);
                                    }
                                } catch (Exception e2) {
                                    if (OAuthView.this.p != null) {
                                        OAuthView.this.p.onError(e2);
                                    }
                                }
                            }
                        });
                    } else if (scheme.startsWith("mailto")) {
                        String packageName = OAuthView.this.getContext().getApplicationContext().getPackageName();
                        String a2 = a(OAuthView.t, OAuthView.u, packageName);
                        String a3 = a(OAuthView.v, OAuthView.w, packageName);
                        String a4 = a(OAuthView.x, OAuthView.y, packageName);
                        URI uri = new URI(str.substring(7));
                        String[] strArr2 = {OAuthView.z};
                        if (uri.getPath() != null) {
                            strArr2 = uri.getPath().split(",");
                        }
                        String query = uri.getQuery();
                        String[] strArr3 = null;
                        if (query != null) {
                            strArr = null;
                            for (String str2 : query.split("&")) {
                                String[] split = str2.split("=");
                                if (split.length == 2) {
                                    if ("subject".equalsIgnoreCase(split[0])) {
                                        a2 = split[1];
                                    } else if ("body".equalsIgnoreCase(split[0])) {
                                        a3 = split[1];
                                    } else if ("cc".equalsIgnoreCase(split[0])) {
                                        strArr3 = split[1].split(",");
                                    } else if ("bcc".equalsIgnoreCase(split[0])) {
                                        strArr = split[1].split(",");
                                    }
                                }
                            }
                        } else {
                            strArr = null;
                        }
                        Intent intent = new Intent("android.intent.action.SEND");
                        intent.setType("text/plain");
                        intent.putExtra("android.intent.extra.EMAIL", strArr2);
                        intent.putExtra("android.intent.extra.SUBJECT", a2);
                        intent.putExtra("android.intent.extra.TEXT", a3);
                        if (strArr3 != null) {
                            intent.putExtra("android.intent.extra.CC", strArr3);
                        }
                        if (strArr != null) {
                            intent.putExtra("android.intent.extra.BCC", strArr);
                        }
                        OAuthView.this.getContext().startActivity(Intent.createChooser(intent, a4));
                    }
                    return true;
                } catch (Exception e2) {
                    if (OAuthView.this.p != null) {
                        OAuthView.this.p.onError(e2);
                    }
                    return false;
                }
            }

            @Override // android.webkit.WebViewClient
            public void onReceivedError(WebView webView, int i3, String str, String str2) {
                EsriServiceException esriServiceException = new EsriServiceException(str);
                if (OAuthView.this.p != null) {
                    OAuthView.this.p.onError(esriServiceException);
                    return;
                }
                throw esriServiceException;
            }

            @Override // android.webkit.WebViewClient
            public void onReceivedSslError(WebView webView, SslErrorHandler sslErrorHandler, SslError sslError) {
                if (OAuthView.this.q != null) {
                    OAuthView.this.q.onReceivedSslError((OAuthView) webView, sslErrorHandler, sslError);
                    return;
                }
                throw new EsriServiceException(EsriSecurityException.UNTRUSTED_SERVER_CERTIFICATE, sslError.toString());
            }

            @Override // android.webkit.WebViewClient
            public void onReceivedHttpAuthRequest(WebView webView, HttpAuthHandler httpAuthHandler, String str, String str2) {
                a(context, httpAuthHandler, str);
            }

            private void a(Context context2, final HttpAuthHandler httpAuthHandler, String str) {
                String packageName = context2.getApplicationContext().getPackageName();
                AlertDialog.Builder builder = new AlertDialog.Builder(context2);
                LinearLayout linearLayout = new LinearLayout(context2);
                linearLayout.setOrientation(1);
                int rgb = Color.rgb(51, 181, (int) TbsListener.ErrorCode.INSTALL_FROM_UNZIP);
                TextView textView = new TextView(context2);
                textView.setText(a(OAuthView.a, OAuthView.b, packageName));
                textView.setGravity(1);
                textView.setTextSize(22.0f);
                textView.setTextColor(rgb);
                textView.setTypeface(textView.getTypeface(), 1);
                textView.setPadding(0, 10, 0, 5);
                linearLayout.addView(textView, -1, -2);
                TextView textView2 = new TextView(context2);
                textView2.setText(str);
                textView2.setTextColor(rgb);
                textView2.setPadding(0, 5, 0, 10);
                textView2.setGravity(1);
                linearLayout.addView(textView2, -1, -2);
                View view = new View(context2);
                view.setBackgroundColor(rgb);
                linearLayout.addView(view, -1, 1);
                final EditText editText = new EditText(context2);
                editText.setPadding(editText.getPaddingLeft(), 20, editText.getPaddingRight(), editText.getPaddingBottom());
                editText.setHint(a(OAuthView.c, OAuthView.d, packageName));
                linearLayout.addView(editText, -1, -1);
                final EditText editText2 = new EditText(context2);
                editText2.setHint(a(OAuthView.e, OAuthView.f, packageName));
                editText2.setInputType(Wbxml.EXT_T_1);
                linearLayout.addView(editText2, -1, -1);
                DialogInterface.OnClickListener onClickListener = new DialogInterface.OnClickListener() { // from class: com.esri.android.oauth.OAuthView.1.2
                    @Override // android.content.DialogInterface.OnClickListener
                    public void onClick(DialogInterface dialogInterface, int i3) {
                        if (i3 == -2) {
                            httpAuthHandler.cancel();
                        } else if (i3 != -1) {
                        } else {
                            httpAuthHandler.proceed(editText.getText().toString(), editText2.getText().toString());
                        }
                    }
                };
                String a2 = a(OAuthView.g, OAuthView.h, packageName);
                final AlertDialog show = builder.setPositiveButton(a2, onClickListener).setNegativeButton(a(OAuthView.i, OAuthView.j, packageName), onClickListener).setView(linearLayout).show();
                show.setCanceledOnTouchOutside(false);
                show.getButton(-1).setEnabled(false);
                TextWatcher textWatcher = new TextWatcher() { // from class: com.esri.android.oauth.OAuthView.1.3
                    @Override // android.text.TextWatcher
                    public void beforeTextChanged(CharSequence charSequence, int i3, int i4, int i5) {
                    }

                    @Override // android.text.TextWatcher
                    public void onTextChanged(CharSequence charSequence, int i3, int i4, int i5) {
                    }

                    @Override // android.text.TextWatcher
                    public void afterTextChanged(Editable editable) {
                        show.getButton(-1).setEnabled(editText.getText().length() > 0 && editText2.getText().length() > 0);
                    }
                };
                editText.addTextChangedListener(textWatcher);
                editText2.addTextChangedListener(textWatcher);
                show.setOnCancelListener(new DialogInterface.OnCancelListener() { // from class: com.esri.android.oauth.OAuthView.1.4
                    @Override // android.content.DialogInterface.OnCancelListener
                    public void onCancel(DialogInterface dialogInterface) {
                        Log.i(a.a, "onCancel");
                        httpAuthHandler.cancel();
                    }
                });
            }

            private String a(String str, String str2, String str3) {
                int identifier = OAuthView.this.getResources().getIdentifier(str, OAuthView.k, str3);
                return identifier == 0 ? str2 : OAuthView.this.getResources().getString(identifier);
            }
        });
        a(a());
    }

    private void a(String str) {
        String str2;
        StringBuilder sb = new StringBuilder();
        sb.append(str);
        sb.append("/oauth2/authorize?client_id=");
        sb.append(this.n);
        sb.append("&response_type=code&redirect_uri=");
        sb.append(m);
        sb.append("&hideCancel=true");
        if (this.r) {
            str2 = "&expiration=" + this.s;
        } else {
            str2 = "";
        }
        sb.append(str2);
        loadUrl(sb.toString());
    }

    /* JADX INFO: Access modifiers changed from: private */
    public UserCredentials a(String str, String str2) throws Exception {
        GetTokenTaskParameters getTokenTaskParameters = new GetTokenTaskParameters(str2, null, this.n);
        return d.a(str, getTokenTaskParameters.b(), getTokenTaskParameters.c(), new com.esri.core.internal.tasks.oauth.a(getTokenTaskParameters, str).execute());
    }

    public void setCallbackListener(CallbackListener<UserCredentials> callbackListener) {
        this.p = callbackListener;
    }

    public void setOnSslErrorListener(OnSslErrorListener onSslErrorListener) {
        this.q = onSslErrorListener;
    }

    public void setPortalUrl(String str) {
        this.o = str;
    }

    @Deprecated
    public void setappID(String str) {
        this.n = str;
    }

    public void setClientId(String str) {
        this.n = str;
    }

    public void setExpiration(int i2) {
        this.s = i2;
        this.r = true;
    }

    public void refresh() {
        a(a());
    }

    /* JADX INFO: Access modifiers changed from: private */
    public String a() {
        if (this.o.endsWith("/sharing/") || this.o.endsWith("/sharing")) {
            return this.o;
        }
        return this.o + "/sharing";
    }
}
